package org.example.user.mapper;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.example.user.dto.CodeDTO;
import org.example.user.entity.SysAclEntity;
import org.example.user.entity.SysRoleAclEntity;
import org.example.user.entity.SysRoleEntity;
import org.example.user.entity.SysRoleUserEntity;

import java.util.List;

public interface CodeMapper extends BaseMapper<CodeDTO> {
    /**
     * select * from sys_role_user su
     * left join sys_role_acl rc on su.role_id = rc.role_id
     * left join sys_acl sa on rc.acl_id = sa.id
     * where su.user_id = 1
     * @return
     */

    default List<CodeDTO> getCode() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(SysAclEntity::getCode, SysAclEntity::getName, SysAclEntity::getId)
                .from(SysRoleUserEntity.class)
                .leftJoin(SysRoleAclEntity.class).on(SysRoleUserEntity::getRoleId, SysRoleAclEntity::getRoleId)
                .leftJoin(SysAclEntity.class).on(SysRoleAclEntity::getAclId, SysAclEntity::getId)
                .where(SysRoleUserEntity::getUserId).eq(StpUtil.getLoginIdAsLong());

        List<CodeDTO> codeDTOS = this.selectListByQuery(queryWrapper);

        return codeDTOS;
    }

}
